package cn.xuqiudong.backend.module.system.main.controller;

import cn.xuqiudong.backend.base.controller.BaseBackendPuredController;
import cn.xuqiudong.backend.base.helper.SaSessionHelper;
import cn.xuqiudong.backend.base.helper.SessionUserInfo;
import cn.xuqiudong.backend.module.system.main.model.request.UserLoginRequest;
import cn.xuqiudong.backend.module.system.main.model.response.ApiLoginResponse;
import cn.xuqiudong.backend.module.system.main.service.AuthService;
import cn.xuqiudong.common.base.model.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 描述:
 *      入口类  如登录 登出等
 * @author Vic.xu
 * @since 2025-02-07 16:54
 */
@RestController
@RequestMapping(value = {"/main"})
public class MainController extends BaseBackendPuredController {

    private AuthService authService;

    @Autowired
    public void setAuthService(AuthService authService) {
        this.authService = authService;
    }

    @GetMapping("/test")
    public BaseResponse<ApiLoginResponse> index(String username, String password){
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUsername(username);
        userLoginRequest.setPassword(password);
        BaseResponse<ApiLoginResponse> login = authService.login(userLoginRequest);
        return login;
    }

    @PostMapping("/login")
    public BaseResponse<ApiLoginResponse> login(@RequestBody  UserLoginRequest userLoginRequest){
        BaseResponse<ApiLoginResponse> login = authService.login(userLoginRequest);
        return login;
    }

    @GetMapping("logout")
    public BaseResponse<Void> logout(){
        authService.logout();
        return BaseResponse.success();
    }

    @GetMapping("/userInfo")
    public BaseResponse<SessionUserInfo> getUserInfo(){
        SessionUserInfo user = SaSessionHelper.getUser();
        return BaseResponse.success(user);
    }
}
